package net.yambal.android.air.roulette.manager
{
	import flash.desktop.NativeApplication;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.SharedObject;
	import flash.utils.ByteArray;
	import flash.utils.flash_proxy;
	
	import mx.collections.ArrayCollection;
	
	import net.yambal.android.air.roulette.RouletteItem;

	public class RouletteManager
	{
		private static var _instance:RouletteManager;
		
		private var _sd_dir:File = null;
		private var _user_app_img_dir:File = null
		
		private var _roulette_items:ArrayCollection = null;
		
		public function RouletteManager()
		{
			var appXml:XML = NativeApplication.nativeApplication.applicationDescriptor;
			var ns:Namespace = appXml.namespace();
			var filename:String = appXml.ns::filename[0];
			
			_sd_dir = File.userDirectory;
			_user_app_img_dir = _sd_dir.resolvePath(filename);
			_user_app_img_dir.createDirectory();
			
			_roulette_items = new ArrayCollection();
			
			var so:SharedObject = SharedObject.getLocal("roulette");
			
			var last_roulette:Array = so.data.last_roulette as Array;
			
			if(last_roulette == null || last_roulette.length <= 1){
				_roulette_items.source = getDefaultTouletteItems();
				
			}else{
				_roulette_items.source = last_roulette;	
			}
			
		}
		
		public static function getInstance():RouletteManager {
			if(_instance == null){
				_instance = new RouletteManager();
			}
			return _instance;
		}
		
		[Bindable]
		public function get items():ArrayCollection{
			return _roulette_items;
		}
		
		public function set items(items:ArrayCollection):void{
			_roulette_items = items;
		}
		
		public function saveItems():void{
			trace("saveItems");
			var so:SharedObject = SharedObject.getLocal("roulette");
			so.data.last_roulette = _roulette_items.toArray();
			so.flush();
		}
		
		private function getDefaultTouletteItems():Array{
			
			var item1:RouletteItem = new RouletteItem;
			item1.label = "1";
			var item2:RouletteItem = new RouletteItem;
			item2.label = "2";
			var item3:RouletteItem = new RouletteItem;
			item3.label = "3";
			var item4:RouletteItem = new RouletteItem;
			item4.label = "4";
			var item5:RouletteItem = new RouletteItem;
			item5.label = "5";
			
			return [item1, item2, item3, item4, item5];
		}
		
		//
		
		public function saveImage(byte_array:ByteArray, prefix:String, exe:String):File{
			
			var file_name:String = prefix + "-" + time_stump + "." + exe;
			var file:File = _user_app_img_dir.resolvePath(file_name);
			
			var stream:FileStream = new FileStream();
			stream.openAsync(file, FileMode.WRITE);
			stream.writeBytes(byte_array);
			stream.close();
			
			return file;
			
		}
		
		private function get time_stump():Number{
			var now:Date = new Date();
			return Date.UTC(now.fullYear, now.month, now.date, now.hours, now.minutes, now.seconds, now.milliseconds);;
		}
	}
}